﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

/// <summary>
/// This class is the webservice which gets information from two textfiles
/// for the active directory groups and the companykeys. The information is requested
/// by the auto-complete widget of Jquery-UI and passed to it via JSON.
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WSautocomplete : System.Web.Services.WebService {

    public WSautocomplete () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
   
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] GetADGroup(string groupname)
    {

        int retvalfr;
        string retMsgrf = "";       

        FSGetFileContent fileCont = new FSGetFileContent();
        fileCont.SourceFile = WbGetParams.ADGroupNamesFile;

        List<string> rtnList = fileCont.rtnValues(out retvalfr, ref retMsgrf);
        rtnList.Sort();

        if (retvalfr == 0)
        {
            Dictionary<string, string> allgroups = new Dictionary<string, string>();
            foreach (string itm in rtnList)
            {
                string[] linearr = itm.Split('^');
                allgroups.Add(linearr[1], linearr[0]);
            }

            List<string> foundgroups = new List<string>();
            foreach (var pair in allgroups)
            {
                Regex grpex = new Regex(@".*" + groupname + ".*", RegexOptions.IgnoreCase);
                Match grpMatch = grpex.Match(pair.Key);
                if (grpMatch.Success)
                {
                    foundgroups.Add(pair.Key + '^' + pair.Value);                    
                }
            }                                   
            
            return foundgroups.ToArray();
        }
        else
        {
            List<string> foundgroups = new List<string>();
            foundgroups.Add("no groups" + '^' + " in cache ");
            
            return foundgroups.ToArray();
        }

    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] GetComKey(string companykey)
    {

        int retvalfr;
        string retMsgrf = "";

        FSGetFileContent fileCont = new FSGetFileContent();
        fileCont.SourceFile = WbGetParams.CompanyKeysFile;

        List<string> rtnList = fileCont.rtnValues(out retvalfr, ref retMsgrf);
        rtnList.Sort();

        if (retvalfr == 0)
        {            
            List<string> foundkeys = new List<string>();
            foreach (string itmkey in rtnList)
            {
                Regex keyex = new Regex(@".*" + companykey + ".*", RegexOptions.IgnoreCase);
                Match keyMatch = keyex.Match(itmkey);
                if (keyMatch.Success)
                {
                    foundkeys.Add(itmkey);
                }                
            }
            return foundkeys.ToArray();
        }
        else
        {
            List<string> foundkeys = new List<string>();
            foundkeys.Add("no keys" + ',' + " in cache ");
            return foundkeys.ToArray();
        }

    }



}
